// pages/activity/business_info.js
import { Activity } from '../../model/activity';
const appInst =  getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    hidden: false,
    info: null,
    date: '',
    week: '',
    sjActivityId: '',
    sjActivityItemId: '',
    time_show: false,
    time_string: '',
    columns: [],
    number: 1,
    pay_loading: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      sjActivityId: options.id,
      date: options.date,
      week: options.week,
    })
    this.getSjActivityInfo()
  },

  // 获取商家活动详情
  async getSjActivityInfo() {
    wx.showLoading({
      title: '加载中',
    });
    const res = await Activity.getSjActivityInfo(this.data.sjActivityId);
    let columns = [];
    res.items.forEach((item)=>{
        columns.push(item.start_time+' - '+item.end_time)
    })
    this.setData({
      info: res,
      columns: columns,
      hidden: true,
    })
    wx.hideLoading()
  },
  /**
   * 点击显示时间
   */
  changeTime() {
    this.setData({
      time_show: !this.data.time_show
    })
  },
  /**
   * 时间切换
   */
  onConfirm(e) {
    const index = e.detail.index;
    this.setData({
      sjActivityItemId: this.data.info.items[index].id,
      time_string: e.detail.value,
    })
    this.changeTime();
  },
  /**
   * 更换数量
   */
  onChange(e) {
    this.setData({
      number: e.detail,
    })
  },
  /**
   * 创建订单信息
   */
  async createOrder(){
    const self = this;
    this.setData({
      pay_loading: true,
    })
    const res = await Activity.createSjActivityOrder( {
      id: this.data.info.id,
      week: this.data.week,
      date: this.data.date,
      sjActivityItemId: this.data.sjActivityItemId,
      number: this.data.number,
      type: 1,
    });
    if(res) {
      appInst.preOrder(res.out_trade_no,function (){
        self.changeLoading();
      });
    } else {
      this.setData({
        pay_loading: false,
      })
    }
  },
  /**
   * 关闭弹框以及加载
   */
  changeLoading() {
    this.setData({
      pay_loading: false,
    });
    wx.hideLoading();
  },
})